package com.example.books2;

import com.example.books2.entity.Book;
import com.example.books2.entity.BorrowRecord;
import com.example.books2.service.LibraryService;
import jakarta.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Scanner;

@SpringBootApplication
public class Books2Application {

	@Autowired
	private LibraryService libraryService;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public static void main(String[] args) {
		SpringApplication.run(Books2Application.class, args);
	}

	@Bean
	public CommandLineRunner commandLineRunner() {
		return args -> {
			// 检查数据库连接
			try {
				jdbcTemplate.execute("SELECT 1");
				System.out.println("数据库连接成功！");

				// 创建备份数据表
				jdbcTemplate.execute("""
					CREATE TABLE IF NOT EXISTS backup_data (
						id BIGINT AUTO_INCREMENT PRIMARY KEY,
						description TEXT,
						backup_date DATETIME NOT NULL
					)
				""");

			} catch (Exception e) {
				System.err.println("数据库连接失败：" + e.getMessage());
				return;
			}

				Scanner scanner = new Scanner(System.in);
				while (true) {
					System.out.println("\n=== 图书管理系统 ===");
					System.out.println("1. 查看所有图书");
					System.out.println("2. 查看借阅记录");
					System.out.println("3. 借书");
					System.out.println("4. 还书");
					System.out.println("5. 备份数据库");
					System.out.println("6. 还原数据库");
					System.out.println("7. 退出");
					System.out.print("请选择操作 (1-7): ");

					String choice = scanner.nextLine();
					try {
						switch (choice) {
							case "1":
								libraryService.showAllBooks();
								break;
							case "2":
								libraryService.showBorrowRecords();
								break;
							case "3":
								borrowBook(scanner);
								break;
							case "4":
								returnBook(scanner);
								break;
							case "5":
								backupDatabase(scanner);
								break;
							case "6":
								restoreDatabase(scanner);
								break;
							case "7":
								System.out.println("感谢使用！再见！");
								return;
							default:
								System.out.println("无效的选择，请重试。");
						}
					} catch (Exception e) {
						System.out.println("操作失败: " + e.getMessage());
						e.printStackTrace();  // 添加这行来打印详细错误信息
					}
				}
		};
	}

	private void borrowBook(Scanner scanner) {
		System.out.print("请输入图书ID: ");
		Long bookId = Long.parseLong(scanner.nextLine());
		System.out.print("请输入用户ID: ");
		Long userId = Long.parseLong(scanner.nextLine());
		boolean success = libraryService.borrowBook(bookId, userId);
		System.out.println(success ? "借书成功！" : "借书失败！");
	}

	private void returnBook(Scanner scanner) {
		System.out.print("请输入借阅记录ID: ");
		Long borrowRecordId = Long.parseLong(scanner.nextLine());
		try {
			libraryService.returnBook(borrowRecordId);
			System.out.println("还书成功！");
		} catch (RuntimeException e) {
			System.out.println("还书失败：" + e.getMessage());
		}
	}

	private void backupDatabase(Scanner scanner) {
		// 显示备份前的数据库状态
		System.out.println("\n=== 备份前数据库状态 ===");
		System.out.println("图书信息：");
		libraryService.showAllBooks();
		System.out.println("\n借阅记录：");
		libraryService.showBorrowRecords();

		// 执行备份
		System.out.print("\n请输入备份说明: ");
		String description = scanner.nextLine();
		libraryService.backupDatabase(description);
		System.out.println("数据库备份成功！");
	}

	private void restoreDatabase(Scanner scanner) {
		// 显示还原前的数据库状态
		System.out.println("\n=== 还原前数据库状态 ===");
		System.out.println("图书信息：");
		libraryService.showAllBooks();
		System.out.println("\n借阅记录：");
		libraryService.showBorrowRecords();

		// 执行还原
		System.out.print("\n请输入备份ID: ");
		Long backupId = Long.parseLong(scanner.nextLine());
		libraryService.restoreDatabase(backupId);
		System.out.println("数据库还原成功！");

		// 显示还原后的数据库状态
		System.out.println("\n=== 还原后数据库状态 ===");
		System.out.println("图书信息：");
		libraryService.showAllBooks();
		System.out.println("\n借阅记录：");
		libraryService.showBorrowRecords();
	}
}
